package com.lxj.sign.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxj.sign.mapper.SignMapper;
import com.lxj.sign.service.SignService;
import com.lxj.utils.common.CommonResult;
import com.lxj.utils.entity.Group;
import com.lxj.utils.entity.Sign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class SignServiceImpl implements SignService {
    @Autowired
    private SignMapper signMapper;

    @Override
    public CommonResult add(Sign sign) {
        int result = signMapper.judge(sign);
        if (result==0){
            return CommonResult.success(signMapper.add(sign));
        }else {
            return CommonResult.failed("该成员已经点过到了");
        }
    }

    @Override
    public CommonResult selectGroup() {
        List<Group> list = signMapper.selectGroup();
        return CommonResult.success(list);
    }

    @Override
    public CommonResult selectAll(Sign sign) {
        PageHelper.startPage(sign.getPageNum(),sign.getPageSize());
        PageInfo<Sign> info = new PageInfo<>(this.signMapper.selectAll(sign));
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",info.getList());
        map.put("number",info.getTotal());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult delete(Integer[] id) {
        int result = signMapper.delete(id);
        return CommonResult.success(result);
    }

    @Override
    public CommonResult findById(Integer id) {
        Sign sign = signMapper.findById(id);
        return CommonResult.success(sign);
    }

    @Override
    public CommonResult edit(Sign sign) {
        int result = signMapper.edit(sign);
        return CommonResult.success(result);
    }
}
